<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品名称" prop="ddName">
        <el-input
          v-model="queryParams.ddName"
          style="width: 200px"
          placeholder="请输入药品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="ddKeyword">
        <el-input
          v-model="queryParams.ddKeyword"
          style="width: 200px"
          placeholder="请输入关键字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品类型" prop="ddType">
        <el-select v-model="queryParams.ddType" placeholder="药品类型" clearable size="small" style="width: 200px">
          <el-option
            v-for="type in ddTypes"
            :key="type.ddType"
            :label="type.ddType"
            :value="type.ddType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产厂家" prop="dmid">
        <el-select v-model="queryParams.dmid" placeholder="生产厂家" clearable size="small" style="width: 200px">
          <el-option
            v-for="manufacturer in manufacturerList"
            :key="manufacturer.dmid"
            :label="manufacturer.dmName"
            :value="manufacturer.dmid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处方类型" prop="ddPrescriptionType">
        <el-select v-model="queryParams.ddPrescriptionType" placeholder="处方类型" clearable size="small" style="width: 200px">
          <el-option
            v-for="prescriptionType in prescriptionTypes"
            :key="prescriptionType.ddPrescriptionType"
            :label="prescriptionType.ddPrescriptionType"
            :value="prescriptionType.ddPrescriptionType"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="ddState">
        <el-select v-model="queryParams.ddState" placeholder="状态" clearable size="small" style="width: 200px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['drug:drugInformation:addDrug']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['drug:drugInformation:updateDrug']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['drug:drugInformation:deleteDrug']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange" border="0px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品ID" align="center" prop="ddid" width="80"/>
      <el-table-column label="药品名称" align="center" prop="ddName"/>
      <el-table-column label="药品编码" align="center" prop="ddCoding" />
      <el-table-column label="关键字" align="center" prop="ddKeyword" />
      <el-table-column label="生产厂家" align="center" prop="druManufacturer.dmName"  width="200"/>
      <el-table-column label="药品类型" align="center" prop="ddType" />
      <el-table-column label="处方类型" align="center" prop="ddPrescriptionType" />
      <el-table-column label="单位" align="center" prop="ddUnit"  width="80"/>
      <el-table-column label="处方价格" align="center" prop="ddPrice" width="120"/>
      <el-table-column label="库存量" align="center" prop="ddNum" width="120"/>
      <el-table-column label="预警值" align="center" prop="ddEarlyWarning"  width="80"/>
      <el-table-column label="换算量" align="center" prop="ddMatrixing"  width="80"/>
      <el-table-column label="状态" align="center" prop="ddState" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['drug:drugInformation:updateDrug']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['drug:drugInformation:deleteDrug']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="药品名称" prop="ddName">
              <el-input v-model="form.ddName" placeholder="请输入药品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品编码" prop="ddCoding" style="margin-left: 60px">
              <el-input v-model="form.ddCoding" placeholder="请输入药品编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="关键字" prop="ddKeyword">
              <el-input v-model="form.ddKeyword" placeholder="请输入关键字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="ddUnit" style="margin-left: 60px">
              <el-input v-model="form.ddUnit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="换算量" prop="ddMatrixing">
              <el-input v-model="form.ddMatrixing" placeholder="请输入换算量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处方价格" prop="ddPrice" style="margin-left: 60px">
              <el-input-number v-model="form.ddPrice" placeholder="请输入处方价格" :min="0"  :precision='2'/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="库存量" prop="ddNum">
              <el-input v-model="form.ddNum" placeholder="请输入库存量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警值" prop="ddEarlyWarning" style="margin-left: 60px">
              <el-input-number v-model="form.ddEarlyWarning" placeholder="请输入预警值" :min="0"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="状态" prop="ddState">
              <el-radio-group v-model="form.ddState">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品类型" prop="ddType" style="margin-left: 60px">
              <el-select v-model="form.ddType" placeholder="请选择药品类型" clearable
                         :style="{width: '100%'}">
                <el-option
                  v-for="type in ddTypes"
                  :key="type.ddType"
                  :label="type.ddType"
                  :value="type.ddType">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="处方类型" prop="ddPrescriptionType">
              <el-select v-model="form.ddPrescriptionType" placeholder="请选择处方类型" clearable
                         :style="{width: '100%'}">
                <el-option
                  v-for="prescriptionType in prescriptionTypes"
                  :key="prescriptionType.ddPrescriptionType"
                  :label="prescriptionType.ddPrescriptionType"
                  :value="prescriptionType.ddPrescriptionType">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="dmid" style="margin-left: 60px">
              <el-select v-model="form.dmid" placeholder="请选择生产厂家" clearable
                         :style="{width: '100%'}">
                <el-option
                  v-for="manufacturer in manufacturerList"
                  :key="manufacturer.dmid"
                  :label="manufacturer.dmName"
                  :value="manufacturer.dmid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {selectManufacturer,selectDdType,selectPrescriptionType,selectAll,addDrug,selectOne,updateDrug,deleteDrug} from "@/api/drug/drugInformation";

  export default {
    name: "drugInformation",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 药品表格数据
        drugList: [],
        // 工厂全部数据
        manufacturerList: [],
        // 药品类型全部数据
        ddTypes: [],
        // 处方类型全部数据
        prescriptionTypes: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          ddName: undefined,
          ddKeyword: undefined,
          ddType: undefined,
          dmid: undefined,
          ddPrescriptionType: undefined,
          ddState: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          ddName: [
            { required: true, message: "药品名称不能为空", trigger: "blur" }
          ],
          ddCoding: [
            { required: true, message: "药品编码不能为空", trigger: "blur" }
          ],
          dmid: [
            { required: true, message: "生产厂家不能为空", trigger: "blur" }
          ],
          ddType: [
            { required: true, message: "药品类型不能为空", trigger: "blur" }
          ],
          ddPrescriptionType: [
            { required: true, message: "处方类型不能为空", trigger: "blur" }
          ],
          ddUnit: [
            { required: true, message: "单位不能为空", trigger: "blur" }
          ],
          ddPrice: [
            { required: true, message: "价格不能为空", trigger: "blur" }
          ],
          ddNum: [
            { required: true, message: "库存量不能为空", trigger: "blur" }
          ],
          ddMatrixing: [
            { required: true, message: "换算量不能为空", trigger: "blur" }
          ],
          ddKeyword: [
            { required: true, message: "关键字不能为空", trigger: "blur" }
          ],
          ddEarlyWarning: [
            { required: true, message: "预警值不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getManufacturer();
      this.getDdTypes();
      this.getPrescriptionTypes();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询所有的生产厂家 */
      getManufacturer() {
        this.loading = true;
        selectManufacturer().then(response => {
          this.manufacturerList = response.rows;
        });
      },
      /** 查询所有的药品类型 */
      getDdTypes() {
        this.loading = true;
        selectDdType().then(response => {
          this.ddTypes = response.rows;
        });
      },
      /** 查询所有的处方类型 */
      getPrescriptionTypes() {
        this.loading = true;
        selectPrescriptionType().then(response => {
          this.prescriptionTypes = response.rows;
        });
      },
      /** 查询药品信息列表 */
      getList() {
        this.loading = true;
        selectAll(this.queryParams).then(response => {
          this.drugList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 生产厂家状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.ddState);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          ddid: undefined,
          ddName: undefined,
          ddCoding: undefined,
          dmid: undefined,
          ddType: undefined,
          ddPrescriptionType: undefined,
          ddUnit: undefined,
          ddPrice: undefined,
          ddNum: undefined,
          ddEarlyWarning: undefined,
          ddMatrixing: undefined,
          ddState: "0",
          ddKeyword: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.ddid)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增药品信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const ddid = row.ddid || this.ids
        selectOne(ddid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改药品信息";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.ddid != undefined) {
              updateDrug(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDrug(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ddids = row.ddid || this.ids;
        this.$confirm('是否确认删除生产厂家编号为"' + ddids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteDrug(ddids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      }
    }
  };
</script>
